<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js - water</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background:#777;
				padding:0;
				margin:0;
				font-weight: bold;
				overflow:hidden;
			}
			#info {
				position: absolute;
				top: 0px;
				width: 100%;
				color: #ffffff;
				padding: 5px;
				font-family:Monospace;
				font-size:13px;
				text-align:center;
			}
			a {
				color: #ffffff;
			}
		</style>

		<script src="js/three.js"></script>
		<script src="js/js/controls/OrbitControls.js"></script>
		<script src="js/js/objects/Reflector.js"></script>
		<script src="js/js/objects/Refractor.js"></script>
		<script src="js/js/objects/Water2.js"></script>
		<script src="js/js/WebGL.js"></script>
		<script src="js/js/libs/dat.gui.min.js"></script>

	</head>
	<body>

		<div id="container"></div>
		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - water
		</div>

		<script>
		if ( WEBGL.isWebGLAvailable() === false ) {
			document.body.appendChild( WEBGL.getWebGLErrorMessage() );
		}
		var scene, camera, clock, renderer, controls, water;
		var torusKnot;
		var params = {
			color: '#ffffff',
			scale: 4,
			flowX: 1,
			flowY: 1
		};
		init();
		animate();
		function init() {
			// scene
			scene = new THREE.Scene();
			// camera
			camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
			camera.position.set( - 15, 7, 15 );
			camera.lookAt( scene.position );
			// clock
			clock = new THREE.Clock();
			// mesh
			var torusKnotGeometry = new THREE.TorusKnotBufferGeometry( 3, 1, 256, 32 );
			var torusKnotMaterial = new THREE.MeshNormalMaterial();
			torusKnot = new THREE.Mesh( torusKnotGeometry, torusKnotMaterial );
			torusKnot.position.y = 4;
			torusKnot.scale.set( 0.5, 0.5, 0.5 );
			scene.add( torusKnot );
			// ground
			var groundGeometry = new THREE.PlaneBufferGeometry( 20, 20 );
			var groundMaterial = new THREE.MeshStandardMaterial( { roughness: 0.8, metalness: 0.4 } );
			var ground = new THREE.Mesh( groundGeometry, groundMaterial );
			ground.rotation.x = Math.PI * - 0.5;
			scene.add( ground );
			var textureLoader = new THREE.TextureLoader();
			textureLoader.load( 'textures/hardwood2_diffuse.jpg', function( map ) {
				map.wrapS = THREE.RepeatWrapping;
				map.wrapT = THREE.RepeatWrapping;
				map.anisotropy = 16;
				map.repeat.set( 4, 4 );
				groundMaterial.map = map;
				groundMaterial.needsUpdate = true;
			} );
			// water
			var waterGeometry = new THREE.PlaneBufferGeometry( 20, 20 );
			water = new THREE.Water( waterGeometry, {
				color: params.color,
				scale: params.scale,
				flowDirection: new THREE.Vector2( params.flowX, params.flowY ),
				textureWidth: 1024,
				textureHeight: 1024
			} );
			water.position.y = 1;
			water.rotation.x = Math.PI * - 0.5;
			scene.add( water );
			// light
			var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
			scene.add( ambientLight );
			var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.6 );
			directionalLight.position.set( - 1, 1, 1 );
			scene.add( directionalLight );
			// renderer
			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setSize( window.innerWidth, window.innerHeight );
			renderer.setPixelRatio( window.devicePixelRatio );
			document.body.appendChild( renderer.domElement );
			//
			controls = new THREE.OrbitControls( camera, renderer.domElement );
			//
			window.addEventListener( 'resize', onResize, false );
		}
		function onResize() {
			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();
			renderer.setSize( window.innerWidth, window.innerHeight );
		}
		function animate() {
			requestAnimationFrame( animate );
			render();
		}
		function render() {
			var delta = clock.getDelta();
			torusKnot.rotation.x += delta;
			torusKnot.rotation.y += delta * 0.5;
			renderer.render( scene, camera );
		}
	</script>

</body>
</html>